package face.pay.api.payter.channel.paytype;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.SendResult;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.face.pay.IPayConfigServer;
import face.pay.api.payter.channel.paytype.message.MessageSender;
import face.pay.constant.OemType;
import face.pay.dao.*;
import face.pay.entity.CpnCardDetailInfo;
import face.pay.entity.OrderInfo;
import face.pay.rocket.MqConfig;
import face.pay.util.MathUtil;
import face.pay.util.ResultMessage;
import face.pay.vo.VCpnCardDetail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public abstract class AbstractPayType {

    @Autowired
    private MessageSender messageSender;

    @Autowired
    public OrderInfoDao orderInfoDao;

    @Autowired
    public OemInfoDao oemInfoDao;

    @Autowired
    public EnterpriseInfoDao enterpriseInfoDao;

    @Autowired
    public StoreInfoDao storeInfoDao;

    @Autowired
    public CpnCardDetailInfoDao cpnCardDetailInfoDao;

    @Autowired
    public IPayConfigServer iWxPayConfigServer;

    public abstract ResultMessage pay(JSONObject json);


    /**
     * 验证订单信息
     * @param orderInfo
     * @param terminalId
     * @param totalFee
     * @return
     */
    public Boolean paySafe(OrderInfo orderInfo,Integer terminalId,Long totalFee){
        Integer orderTerminalId = orderInfo.getTerminalId();

        if(orderTerminalId.equals(terminalId)){

                if(totalFee.equals(orderInfo.getTotalFee())){
                    return true;
                }

        }
        return  false;
    }


    public String payCardSafe(OrderInfo orderInfo,Long cardDetailId, Integer terminalId,Long totalFee,Long actFee){

        Integer orderTerminalId = orderInfo.getTerminalId();

        if(orderTerminalId.equals(terminalId)){
            if(!totalFee.equals(orderInfo.getTotalFee())){
                log.error("支付金额和下单金额不匹配");
               return  "支付金额和下单金额不匹配";
            }else {
                if(cardDetailId!=null && cardDetailId>0){
                    /**
                     * 查询当前优惠券是否可以使用
                     */
                    VCpnCardDetail cardDetail = this.cpnCardDetailInfoDao.getVCpnById(cardDetailId);

                    if(cardDetail==null){
                        log.error("未找到此次需要核销的优惠券或优惠券已被核销");
                        return  "未找到此次需要核销的优惠券或优惠券已被核销";
                    }else{
                        /**
                         * 卡券折扣
                         */
                        Double c  = MathUtil.sub(new Double(1),cardDetail.getDiscount());

                        /**
                         * 应付款
                         */
                        Long money = MathUtil.profit(totalFee,c);

                        if(!(actFee.compareTo(money)==0)){
                            log.error("用户实际付款和优惠券折扣 不匹配");

                            return "用户实际付款和优惠券折扣 不匹配";

                        }
                    }
                }

            }

        }
       return  null;
    }

    public String paySafe(Integer terminalId,Long cardDetailId,Long totalFee,Long actFee){

         if(cardDetailId!=null && cardDetailId>0){
            /**
             * 查询当前优惠券是否可以使用
             */
            VCpnCardDetail cardDetail = this.cpnCardDetailInfoDao.getVCpnById(cardDetailId);

            if(cardDetail==null){
                log.error("未找到此次需要核销的优惠券或优惠券已被核销");
                return  "未找到此次需要核销的优惠券或优惠券已被核销";
            }else{
                /**
                 * 卡券折扣
                 */
                Double c  = MathUtil.sub(new Double(1),cardDetail.getDiscount());

                /**
                 * 应付款
                 */
                Long money = MathUtil.profit(totalFee,c);

                if(!(actFee.compareTo(money)==0)){
                    log.error("用户实际付款和优惠券折扣 不匹配");

                    return "用户实际付款和优惠券折扣 不匹配";

                }
            }
            }else {
             if(totalFee.compareTo(actFee)!=0){
                 return "实际付款金额错误";
             }
         }


        return  null;
    }


    public void sendAllMessage(OrderInfo orderInfo,String type){

        try{

            /**
             * 分佣消息
             */
            log.info("发送分佣消息");
            this.messageSender.sendMessage(orderInfo, type);

            Long cardDetailId = orderInfo.getCardDetailId();

            if(cardDetailId!=null && cardDetailId>0){
                log.info("修改优惠券使用状态");

                CpnCardDetailInfo cpnCardDetailInfo = this.cpnCardDetailInfoDao.getById(cardDetailId);

                if(cpnCardDetailInfo!=null){
                    cpnCardDetailInfo.setStatus(3);
                    this.cpnCardDetailInfoDao.update(cpnCardDetailInfo);
                }
                log.info("发送分账消息");
                this.messageSender.sendSharingMessage(orderInfo, type);
            }


        }catch (Exception e){
            e.printStackTrace();
        }


    };


}
